package com.doucat.support.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import java.lang.reflect.Field;

/**
 * -----------------------------------------------------------------
 * Copyright (C) 2014-2016, by your company, All rights reserved.
 * -----------------------------------------------------------------
 * File: BaseDialog.java
 * Author: win7
 * Version: V100R001C01
 * Create: 2019/6/17 19:14
 * Changes (from 2019/6/17)
 * -----------------------------------------------------------------
 * 2019/6/17 : Create BaseDialog.java (win7);
 * -----------------------------------------------------------------
 */
public abstract class BaseDialog extends DialogFragment {
    public String TAG=getClass().getSimpleName();
    public void show(FragmentActivity fragmentActivity,FragmentManager manager){
        if((!isAdded())&&(!fragmentActivity.isDestroyed())){
//            showNow(fragmentActivity.getSupportFragmentManager(),TAG);
            if(!isShowing()){
                showAllowingStateLoss(manager,TAG);
//                show(manager,TAG);
            }
        }
    }

    public void showAllowingStateLoss(FragmentManager manager, String tag) {
        try {
            Field dismissed = DialogFragment.class.getDeclaredField("mDismissed");
            dismissed.setAccessible(true);
            dismissed.set(this, false);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        try {
            Field shown = DialogFragment.class.getDeclaredField("mShownByMe");
            shown.setAccessible(true);
            shown.set(this, true);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        FragmentTransaction ft = manager.beginTransaction();
        ft.add(this, tag);
        ft.commitNowAllowingStateLoss();
    }

    @Override
    public void dismiss() {
        if(this.isAdded()){
            super.dismissAllowingStateLoss();
        }
    }

    public boolean isShowing() {
        Dialog dialog=getDialog();
        if(dialog==null){
            return false;
        }else{
            return getDialog().isShowing();
        }
    }

    public void setCanceledOnTouchOutside(boolean canceledOnTouchOutside) {
        Dialog dialog=getDialog();
        if(dialog!=null){
            dialog.setCanceledOnTouchOutside(canceledOnTouchOutside);
        }
    }


    public BaseDialog setPbCancelable(boolean cancelable) {
        setCancelable(cancelable);
        return this;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, 0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT);
        getDialog().getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getDialog().getWindow().setAttributes(params);
        return onLoadLayout(inflater, container, savedInstanceState);
    }
    public abstract View onLoadLayout(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);
}
